<?php
namespace Netshell\Controllers;

class BaseResource extends Base
{
	// Display a listing of the resource.
	// GET				/resource
	public function index()
	{
		$model = $this->callModel('all');
		$this->addViewData(['model' => $model]);
		return $this->showView();
	}


	// Show the form for creating a new resource.
	// GET				/resource/create
	public function create()
	{
		return $this->showView();
	}


	// Store a newly created resource in storage.
	// POST				/resource
	public function store()
	{
		$model = new $this->classModel;
		$this->fillModel($model)->save();
		return \Redirect::to($this->getUrl($model->id));
	}


	// Display the specified resource.
	// GET				/resource/{resource}
	public function show($id)
	{
		$this->model = $this->findModel($id)->toObject();
		return $this->showView();
	}


	// Show the form for editing the specified resource.
	// GET				/resource/{resource}/edit
	public function edit($id)
	{
		$model = $this->findModel($id)->toObject();
		$this->addViewData(['model' => $model]);
		return $this->showView();
	}


	// Update the specified resource in storage.
	// PUT/PATCH	/resource/{resource}
	public function update($id)
	{
		$model = $this->findModel($id);
		$this->fillModel($model)->save();
		return \Redirect::to($this->getUrl($model->id));
	}


	// Remove the specified resource from storage.
	// DELETE			/resource/{resource}	
	public function destroy($id)
	{
		if(!is_array($id))
			$id = array($id);
		
		foreach($id as $i)
		{
			$model = $this->findModel($i);
			$model->delete();
		}
		return \Redirect::to($this->getUrl());
	}
	
	// =========================================================

	protected $viewData = array();
	protected $base;
	
	public function __construct()
	{
		$this->base = $this;
		$this->name = explode('\\', get_called_class());
		$this->name = end($this->name);
		$this->fullName = __NAMESPACE__ . '\\' . $this->name;
		$this->namelc = strtolower($this->name);
		$this->baseNS = explode('\\', __NAMESPACE__);
		array_pop($this->baseNS);
		$this->baseNS = implode('\\', $this->baseNS);
		$this->classModel = "$this->baseNS\\Models\\$this->name";
		$this->url = '/' . $this->namelc . 's/%s';
		
		$viewData['actionLinks'] = array();
	}
	
	public function addViewData($data)
	{
		foreach($data as $k=>$v)
			$this->viewData[$k] = $v;
	}
	
	public function showView()
	{
		$caller = debug_backtrace();
		$caller = $caller[1]['function'];
		return \View::make("$this->namelc.$caller", $this->viewData)->render();
	}
	
	private function getUrl($id = '')
	{
		return sprintf($this->url, $id);
	}
	
	protected function findModel($id)
	{
		return $this->callModel('find', $id)[0];
	}
	
	private function callModel($method, $args = null)
	{
		$cuf = [$this->classModel, $method];
		if($args != null)
		{
			if(!is_array($args))
				$args = array($args);
			return call_user_func($cuf, $args);
		}
		return call_user_func($cuf);
	}
	
	private function fillModel($instance)
	{
		$allFields = $instance->getAllColumnsNames();

		$instance->fill(\Input::only($allFields));
		return $instance;
	}
	
}
